package com.zzyl.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import javax.swing.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

@Slf4j
public class PDFUtil {

    public static String pdfToString(InputStream inputStream) {
        try (
                InputStream is = inputStream;
                PDDocument document = PDDocument.load(is);
        ) {
            PDFTextStripper pdfTextStripper = new PDFTextStripper();
            return pdfTextStripper.getText(document);
        }catch (IOException e){
            log.error("pdf转换字符串异常", e);
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        String pdfContent = PDFUtil.pdfToString(new FileInputStream("D:\\Project-1\\Day06.智能评估-集成AI大模型\\资料\\体检报告样例\\体检报告-刘爱国-男-69岁.pdf"));
        System.out.println("pdfContent = " + pdfContent);
    }
}
